package classes.zihol.utils
{
    /**
     *  
     * @author zihol.luo
     * @date 2012年1月5日 13:11:50
     * 基本满足我的需要
     * 我不知道效率如何,欢迎交流
     * @mail:nufei5354@163.com
     * @qq:909630866
     */    
    public class Xml2Obj
    {
        
        public function Xml2Obj() { }
        public static function parseXML(xml:XML,level:int=0):Object
        {
            var o:Object={};
            //            var t:String = '';
            //            for (var i2:int = 0; i2 < level; i2++) t+='==';
            //            t+='>';
            for (var i:int = 0; i < xml.children().length(); i++) 
            {
                if(xml.children()[i].nodeKind()=='text')
                {
                    trace(xml.localName()+'...'+xml.children()[i].toString());
                    o['value'] = xml.children()[i].toString(); // 一般我都用value不然就没有引用名了
                }else if(xml.children()[i].nodeKind()=='element')
                {
                    if(o[xml.children()[i].localName()]!=null)
                    {                    
                        if(!(o[xml.children()[i].localName()] is Array)){
                            var u:* = o[xml.children()[i].localName()];
                            o[xml.children()[i].localName()]=[];
                            o[xml.children()[i].localName()].push(u);
                        }
                        o[xml.children()[i].localName()].push(parseXML(xml.children()[i],++level));
                    }else{
                        o[xml.children()[i].localName()] = parseXML(xml.children()[i],++level);
                    }
                }
            }// end for
            for (var k:int = 0; k < xml.attributes().length(); k++) 
            {
                o[xml.attributes()[k].localName()] = xml.attributes()[k].toString();
            }
            return o;
        }
    }
}